package com.mtnz.njk.controller;

import com.mtnz.njk.Util.DeleteFileUtil;
import com.mtnz.njk.entity.StartImgEntity;
import com.mtnz.njk.result.ResponseMessage;
import com.mtnz.njk.result.ResponseResult;
import com.mtnz.njk.service.StartImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Auther: 范保林
 * @Date: 2019\7\6 0014 15:07
 * @Description: 启动页展示图控制类
 */
@RequestMapping("/startImg")
@RestController
public class StartImgController {

    @Autowired
    StartImgService startImgService;

    /**
     * @api {post} /startImg/showStartImg 展示app首页图片（根据图片名字动态展示）
     * @apiGrop showStartImg
     * @apiParam {String} StartImgEntity 启动图实体类
     */
    @PostMapping("/showStartImg")
    public ResponseResult showStartImg(@RequestBody StartImgEntity startImg) throws Exception {
        // 查询当天应该展示的图片
        List<StartImgEntity> startImgEntities = startImgService.showStartImg(startImg);
        if (startImgEntities != null && startImgEntities.size() > 0) {
            return new ResponseResult(ResponseMessage.OK,startImgEntities);
        } else {
            // 否则展示默认图片
            String imgUrl = DeleteFileUtil.localpath;
            return new ResponseResult(ResponseMessage.OK, imgUrl);
        }
    }

    /**
     * @api {post} /startImg/queryStartImg 查询所有启动图片
     * @apiGrop queryStartImg
     */
    @PostMapping("/queryStartImg")
    public ResponseResult queryStartImg(){
        List<StartImgEntity> startImgEntities = startImgService.queryStartImg();
        return new ResponseResult(ResponseMessage.OK,startImgEntities);
    }

    /**
     * @api {post} /startImg/saveStartImg 保存启动图
     * @apiGrop saveStartImg
     * @apiParam {String} file 图片名称
     */
    @PostMapping("/saveStartImg")
    public ResponseResult saveStartImg(MultipartFile file) throws Exception{
        if(file == null || file.equals("")){
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        }
        return startImgService.saveStartImg(file);
    }

    /**
     * @api {post} /startImg/delStartImg 删除启动图
     * @apiGrop delStartImg
     * @apiParam {String} startImgId 启动图片id
     */
    @PostMapping("/delStartImg")
    public ResponseResult delStartImg(String startImgId) throws Exception{
        if(startImgId == null || startImgId.equals("")){
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        }
        return  startImgService.delStartImg(startImgId);
    }
}
